
<div class="row-fluid">
    <div class="box paint color_11">
        <div class="title">
            <h4> <span><?php echo __('Add Distributions'); ?></span> </h4>
        </div>
        <!-- End .title -->
        
        <div class="content">
             <div id="wizard">
                <ul class="nav nav-tabs">
                  <li class="active"><a href="#step-1" data-toggle="tab">Passo 1: Definir dados inicias</a></li>
                  <li><a href="#step-2" data-toggle="tab" id="step2">Passo 2: Definir situação dos professores</a></li>
                  <li><a href="#step-3" data-toggle="tab" id="step3">Passo 3: Montar distribuição</a></li>
                </ul>
                <?php echo $this->Form->create('Distribution', array('class' => 'form-horizontal row-fluid', 'id' => 'distribution')); ?>
                <div class="tab-content">
                
                <div class="tab-pane fade in active" id="step-1">
                    
                        <div class="form-row control-group row-fluid">
                            <label class="control-label span3" for="normal-field"><?php echo __('Name') ?></label>
                            <div class="controls span7">
                                <?php echo $this->Form->input('name', array('div' => false, 'label' => false, 'class' => 'row-fluid')); ?>
                            </div>
                        </div>
                        <div class="form-row control-group row-fluid">
                            <label class="control-label span3" for="normal-field"><?php echo __('Year') ?></label>
                            <div class="controls span7">
                                <?php echo $this->Form->input('year', array('div' => false, 'label' => false, 'class' => 'row-fluid')); ?>
                            </div>
                        </div>

                        <div class="form-actions row-fluid">
                            <div class="span7 offset3">
                                <button class="btn btn-primary" id="next"><?php echo __('Next Step') ?></button>
                            </div>
                        </div>
                    
                </div>
                <div class="tab-pane fade" id="step-2">
                    <table id="datatable" class="responsive table table-striped table-bordered" style="width:100%;margin-bottom:0; ">
                        <thead>
                            <tr>
                                <th><?php echo __('Name') ?></th>
                                <th><?php echo __('Situation on 1º Semester') ?></th>
                                <th><?php echo __('Situation on 2º Semester') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="checkAll">
                                <td></td>
                                <td>
                                    <div class="row-fluid">
                                        <div class="span12"><button name="1" class="btn btn-primary" >Selecionar/Deselecionar todos</button></div>
                                    </div>
                                </td>
                                <td>
                                    <div class="row-fluid">
                                        
                                        <div class="span12"><button name="2" class="btn btn-primary" >Selecionar/Deselecionar todos</button></div>
                                    </div>
                                </td>
                            </tr>
                            <?php
                            foreach ($teachers as $teacher): ?>
                            <tr>
                                <td><?php echo h($teacher['Teacher']['name']); ?>&nbsp;</td>
                                <td>
                                    <div class="row-fluid">
                                        <label class="control-label span3" for="normal-field"><?php echo __('Available') ?></label>
                                        
                                        <div class="span9"><input rel="1" type="checkbox" <?php echo (isset($results[$teacher['Teacher']['id']]) && $results[$teacher['Teacher']['id']]->getSituation(1))?"checked":"" ?> name="<?php echo h($teacher['Teacher']['id']); ?>[0]" /></div>
                                    </div>
                                </td>
                                <td> 
                                    <div class="row-fluid">
                                        <label class="control-label span3" for="normal-field"><?php echo __('Available') ?></label>
                                        
                                        <div class="span9"><input rel="2" type="checkbox" <?php echo (isset($results[$teacher['Teacher']['id']]) && $results[$teacher['Teacher']['id']]->getSituation(2))?"checked":"" ?> name="<?php echo h($teacher['Teacher']['id']); ?>[1]" /></div>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                    <div class="form-actions row-fluid">
                        <div class="span7 offset3">
                            <button class="btn btn-primary" id="next2"><?php echo __('Next Step') ?></button>
                        </div>
                    </div>
                </div>
                <div class="tab-pane fade" id="step-3">
                    <div class="row-fluid fluid">
                        <div class="span12 well pagination-centered">
                            <button type="submit" class="btn btn-primary btn-large" id="submit">Criar a distribuição</button>
                            <button type="submit" class="btn btn-primary btn-large" id="save">Salvar e finalizar</button>
                        </div>
                        
                    </div>
                    <div class="row-fluid fluid">
                        <div class="span12 well pagination-centered" id="load" style="display:none">
                            <div id="loadingAjax" ><img src="<?php echo $this->Html->url('/tema/') ?>img/ajax-loader.gif"></div>
                        </div>
                        
                        <div class="span12 well pagination-centered" id="dist">
                            <input type="hidden" name="isUpdate" value="<?php echo $id ?>" />
                            <?php


if(isset($results))
{
    foreach ($results as $key => $teacher):
?>
<div class="row-fluid">
    <div class="span12" style="text-align: left;">
        <blockquote style="border-bottom: 1px solid #f5f5f5;">
            <p><h5 style="font-size: 36px;"><?php echo $teachers[$key]['Teacher']['name'] ?></h5></p>
        </blockquote>
    </div>
</div>
<div class="row-fluid">
    <div class="row-fluid box color_24 title_medium height_medium bar_stats paint_hover ">
      <div class="title">
      </div>
      <!-- End .title -->
      <div class="content row-fluid fluid numbers">
        <div class="span3 stats shown_left" style="margin-top: 20px; text-align: left;">
            <p>
            Peso médio dado: <?php echo $weightsBefore[$teachers[$key]['Teacher']['id']] ?><br />
            Peso médio atribuído: <?php echo $weightsAfter[$teachers[$key]['Teacher']['id']] ?><br />
            Carga Horária mínima 1º semestre: <?php echo $teachers[$key]['Teacher']['workload'] ?> horas<br />
            Carga Horária mínima 2º semestre: <?php echo $teachers[$key]['Teacher']['workload'] ?> horas<br />
            Carga Horária Adicional: <?php echo $teachers[$key]['Teacher']['additional_workload'] ?> horas<br />
          </p>
        </div>
        <div class="span3 shown_left hidden_phone">
          <div class="row-fluid fluid">
            <div class="span6">
              <div class="description">Atribuídas</div>
              <h2 class="value"><?php echo $teacher->workLoadTotalRgt-$teacher->getWorkloadRgt() ?> Horas</h2>
              <div class="progress small">
                <div class="bar white" style="width: 100%;"></div>
              </div>
              <div class="description">1º Semestre</div>
            </div>
            <div class="span6 full">
              <div class="description text_color_dark">Restam</div>
              <h2 class="value text_color_dark"><?php echo $teacher->getWorkloadRgt() ?> Horas</h2>
              <div class="progress small">
                <div class="bar " style="width: 0%;"></div>
              </div>
            </div>
          </div>
        </div>
        <div class="span3 shown_left hidden_phone">
          <div class="row-fluid fluid">
            <div class="span6">
              <div class="description">Atribuídas</div>
              <h2 class="value"><?php echo $teacher->workLoadTotalLft-$teacher->getWorkloadLft() ?> Horas</h2>
              <div class="progress small">
                <div class="bar white" style="width: 100%;"></div>
              </div>
              <div class="description">2º Semestre</div>
            </div>
            <div class="span6 full">
              <div class="description text_color_dark">Restam</div>
              <h2 class="value text_color_dark"><?php echo $teacher->getWorkloadLft() ?> Horas</h2>
              <div class="progress small">
                <div class="bar " style="width: 0%;"></div>
              </div>
            </div>
          </div>
        </div>
        <div class="span2 total_days" style="float:right; text-align:right">
          <div class="row-fluid">
            <div class="span6 total_clicks">
              <h1 class="value"><?php echo (($teacher->workLoadTotalLft-$teacher->getWorkloadLft()) + ($teacher->workLoadTotalRgt-$teacher->getWorkloadRgt()))/2 ?></h1>
              <div class="description mt15">Carga horária média</div>
            </div>
          </div>
        </div>
      </div>
      <!-- End .row-fluid --> 
      <!-- End .content --> 
    </div>
    <!-- End .box --> 
    
  </div>
<?php if(count($teacher->getDisciplines())) : ?>
    <table  class="responsive table table-striped table-bordered" style="margin-bottom: 30px;" id="<?php echo $teacher->getId(); ?>1">
        <thead>
            <tr>
                <th><?php echo __('Semester') ?></th>
                <th><?php echo __('Discipline') ?></th>
                <th><?php echo __('W. Sem.') ?></th>
                <th><?php echo __('W. Theoric') ?></th>
                <th><?php echo __('W. Practice') ?></th>
                <th><?php echo __('Team') ?></th>
                <th><?php echo __('Course') ?></th>
                <th><?php echo __('Weight') ?></th>
                <th><?php echo __('Select Teacher') ?></th>
                <th><?php echo __('Fixed') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($teacher->getDisciplines() as $discipline): ?>
            <?php if($discipline->getSemester() == 1): ?>
                <tr>
                    <td rel="semester"><?php echo $disciplines[$discipline->getId()]['Discipline']['semester'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Team']['name'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['total_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['theoretical_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['practice_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['team'] ?>&nbsp;</td>
                    <?php $courses = array(); ?>
                    <?php foreach ($disciplines[$discipline->getId()]['Courses'] as $course) : 
                        $courses[] = $course['acronym'];
                    endforeach; ?>
                    <td><?php echo implode(" + ", $courses) ?>&nbsp;</td>
                    <td rel="weight"><?php echo $teacher->getWeight($discipline->getId()) ?>&nbsp;</td>
                     <td ><?php echo $this->Form->select('_selectteacher', $list1, array('class' => 'selectTeacher')) ?>&nbsp;</td>
                     <td><input rel="check" type="checkbox" name="data[Fixed][<?php echo $teacher->getId(); ?>][<?php echo $discipline->getId(); ?>]" <?php echo (isset($fixed[$teacher->getId()][$discipline->getId()]))?'checked':''; ?> />&nbsp;</td>
                    <input type="hidden" rel="hidden" name="data[Assignment][<?php echo $teacher->getId(); ?>][]" value="<?php echo $discipline->getId(); ?>" />
                </tr>
            <?php endif ?>
        <?php endforeach; ?>
        </tbody>
    </table>
    <table class="responsive table table-striped table-bordered" style="margin-bottom: 30px;" id="<?php echo $teacher->getId(); ?>2">
        <thead>
            <tr>
                <th><?php echo __('Semester') ?></th>
                <th><?php echo __('Discipline') ?></th>
                <th><?php echo __('W. Sem.') ?></th>
                <th><?php echo __('W. Theoric') ?></th>
                <th><?php echo __('W. Practice') ?></th>
                <th><?php echo __('Team') ?></th>
                <th><?php echo __('Course') ?></th>
                <th><?php echo __('Weight') ?></th>
                <th><?php echo __('Select Teacher') ?></th>
                <th><?php echo __('Fixed') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($teacher->getDisciplines() as $discipline): ?>
            <?php if($discipline->getSemester() == 2): ?>
                <tr>
                    <td rel="semester"><?php echo $disciplines[$discipline->getId()]['Discipline']['semester'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Team']['name'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['total_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['theoretical_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['practice_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['team'] ?>&nbsp;</td>
                    <?php $courses = array(); ?>
                    <?php foreach ($disciplines[$discipline->getId()]['Courses'] as $course) : 
                        $courses[] = $course['acronym'];
                    endforeach; ?>
                    <td><?php echo implode(" + ", $courses) ?>&nbsp;</td>
                    <td rel="weight"><?php echo $teacher->getWeight($discipline->getId()) ?>&nbsp;</td>
                     <td ><?php echo $this->Form->select('_selectteacher', $list2, array('class' => 'selectTeacher')) ?>&nbsp;</td>
                     <td><input rel="check" type="checkbox" name="data[Fixed][<?php echo $teacher->getId(); ?>][<?php echo $discipline->getId(); ?>]" <?php echo (isset($fixed[$teacher->getId()][$discipline->getId()]))?'checked':''; ?> />&nbsp;</td>
                    <input type="hidden" rel="hidden" name="data[Assignment][<?php echo $teacher->getId(); ?>][]" value="<?php echo $discipline->getId(); ?>" />
                </tr>
            <?php endif ?>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php else: ?>
    <table class="responsive table table-striped table-bordered" style="margin-bottom: 30px;" id="<?php echo $teacher->getId(); ?>1">
        <thead>
            <tr>
                <th><?php echo __('Semester') ?></th>
                <th><?php echo __('Discipline') ?></th>
                <th><?php echo __('W. Sem.') ?></th>
                <th><?php echo __('W. Theoric') ?></th>
                <th><?php echo __('W. Practice') ?></th>
                <th><?php echo __('Team') ?></th>
                <th><?php echo __('Course') ?></th>
                <th><?php echo __('Weight') ?></th>
                <th><?php echo __('Select Teacher') ?></th>
                <th><?php echo __('Fixed') ?></th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <table class="responsive table table-striped table-bordered" style="margin-bottom: 30px;" id="<?php echo $teacher->getId(); ?>2">
        <thead>
            <tr>
                <th><?php echo __('Semester') ?></th>
                <th><?php echo __('Discipline') ?></th>
                <th><?php echo __('W. Sem.') ?></th>
                <th><?php echo __('W. Theoric') ?></th>
                <th><?php echo __('W. Practice') ?></th>
                <th><?php echo __('Team') ?></th>
                <th><?php echo __('Course') ?></th>
                <th><?php echo __('Weight') ?></th>
                <th><?php echo __('Select Teacher') ?></th>
                <th><?php echo __('Fixed') ?></th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
<?php endif; ?>
<?php
    endforeach;
}
?>
<div class="row-fluid">
    <div class="span12" style="text-align: left;">
        <blockquote style="border-bottom: 1px solid #f5f5f5;">
            <p>Disciplinas não alocadas</p>
        </blockquote>
    </div>
</div>
<?php
if(isset($rest1))
{
?>

<table class="responsive table table-striped table-bordered" style="margin-bottom: 30px;" id="rest1">
    <thead>
            <tr>
                <th><?php echo __('Semester') ?></th>
                <th><?php echo __('Discipline') ?></th>
                <th><?php echo __('W. Sem.') ?></th>
                <th><?php echo __('W. Theoric') ?></th>
                <th><?php echo __('W. Practice') ?></th>
                <th><?php echo __('Team') ?></th>
                <th><?php echo __('Course') ?></th>
                <th><?php echo __('Select Teacher') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($rest1 as $discipline): ?>
                <tr>
                    <td rel="semester"><?php echo $disciplines[$discipline->getId()]['Discipline']['semester'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Team']['name'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['total_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['theoretical_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['practice_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['team'] ?>&nbsp;</td>
                    <?php $courses = array(); ?>
                    <?php foreach ($disciplines[$discipline->getId()]['Courses'] as $course) : 
                        $courses[] = $course['acronym'];
                    endforeach; ?>
                    <td><?php echo implode(" + ", $courses) ?>&nbsp;</td>
                    <td rel="weight" style="display: none">&nbsp;</td>
                    <td ><?php echo $this->Form->select('_selectteacher', $list1, array('class' => 'selectTeacher')) ?>&nbsp;</td>
                    <td style="display: none"><input rel="check" type="checkbox" <?php echo (isset($fixed[$teacher->getId()][$discipline->getId()]))?'checked':''; ?> />&nbsp;</td>
                    <input type="hidden" rel="hidden" style="display: none" value="<?php echo $discipline->getId(); ?>" />
                </tr>
        <?php endforeach; ?>
        </tbody>
</table>
<?php
}
if(isset($rest2))
{
?>
<table class="responsive table table-striped table-bordered" style="margin-bottom: 30px;" id="rest2">
    <thead>
            <tr>
                <th><?php echo __('Semester') ?></th>
                <th><?php echo __('Discipline') ?></th>
                <th><?php echo __('W. Sem.') ?></th>
                <th><?php echo __('W. Theoric') ?></th>
                <th><?php echo __('W. Practice') ?></th>
                <th><?php echo __('Team') ?></th>
                <th><?php echo __('Course') ?></th>
                <th><?php echo __('Select Teacher') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($rest2 as $discipline): ?>
                <tr>
                    <td rel="semester"><?php echo $disciplines[$discipline->getId()]['Discipline']['semester'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Team']['name'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['total_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['theoretical_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['practice_workload'] ?>&nbsp;</td>
                    <td><?php echo $disciplines[$discipline->getId()]['Discipline']['team'] ?>&nbsp;</td>
                    <?php $courses = array(); ?>
                    <?php foreach ($disciplines[$discipline->getId()]['Courses'] as $course) : 
                        $courses[] = $course['acronym'];
                    endforeach; ?>
                    <td><?php echo implode(" + ", $courses) ?>&nbsp;</td>
                    <td rel="weight" style="display: none">&nbsp;</td>
                    <td ><?php echo $this->Form->select('_selectteacher', $list2, array('class' => 'selectTeacher')) ?>&nbsp;</td>
                    <td style="display: none"><input rel="check" type="checkbox" <?php echo (isset($fixed[$teacher->getId()][$discipline->getId()]))?'checked':''; ?> />&nbsp;</td>
                    <input type="hidden" rel="hidden" style="display: none" value="<?php echo $discipline->getId(); ?>" />
                </tr>
        <?php endforeach; ?>
        </tbody>
</table>
<?php
}
?>
                        </div>
                    </div>
                </div>
                
              </div>
              <?php $this->Form->end() ?>
            </div>
           
        </div>
        <!-- End .content --> 
    </div>
    <!-- End box --> 
</div>
<!-- End .row-fluid -->
 <?php
    $this->Js->get("#submit");

    echo $this->Js->event('click', $this->Js->request(
            array('action' => 'runGraph'),
            array(
                'async' => true,
                'method' => 'post',
                
                'dataExpression' => true,
                'data' => $this->Js->serializeForm(array('isForm' => false, 'inline' => true)),
                'evalScripts' => true,
                'update' => '#dist',
                'before' => '$("#load").fadeIn()',
                'complete' => '$("#load").fadeOut();',
                'type' => 'json'
                )
        ));
    
 ?>

 
<?php

    $this->Js->get('.selectTeacher');

    echo $this->Js->event('change',
        "
            var semester = $(this).parent().parent().find('td[rel=\"semester\"]').text();
            var tr = $(this).parent().parent();
            var table = $(this).parent().parent().parent();
            if($(this).val() != 0)
                var target = $('#' + $(this).val() + '' + parseInt(semester));
            else
                var target = $('#rest' + parseInt(semester));

            tr.appendTo(target);
            if($(this).val() != 0)
            {
                tr.find('input[rel=\"hidden\"]').show();
                tr.find('input[rel=\"check\"]').parent().show();
                tr.find('td[rel=\"weight\"]').show();
                tr.find('td[rel=\"weight\"]').text('0');
                tr.find('input[rel=\"hidden\"]').attr('name', 'data[Assignment]['+$(this).val()+'][]')
                tr.find('input[rel=\"check\"]').attr('name', 'data[Fixed]['+$(this).val()+']['+tr.find('input[rel=\"hidden\"]').val()+']')   
                tr.find('input[rel=\"check\"]').attr('checked', true)
            }else{
                tr.find('input[rel=\"hidden\"]').hide();
                tr.find('input[rel=\"check\"]').parent().hide();
                tr.find('td[rel=\"weight\"]').hide();
                tr.find('input[rel=\"hidden\"]').attr('name', '')
                tr.find('input[rel=\"check\"]').attr('name', '')
            }

        "
        );

    echo $this->Js->writeBuffer();
?>